﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Winista.Text.HtmlParser.Lex;
using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Util;
using Winista.Text.HtmlParser.Filters;
using System.Net;
using System.Text;
using YuuzleComic.DBUtility;
using YuuzleComic.BLL;
using System.Data;
using Winista.Text.HtmlParser.Tags;
using HtmlAgilityPack;
using System.Drawing;

public partial class admin_collect_author : YuuzleComic.Web.UI.ManagePage
{
    diqu diqubll = new diqu();
    zz zzbll = new zz();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataTable diqudt = diqubll.GetAllList().Tables[0];
        HtmlWeb web = new HtmlWeb();
        HtmlDocument doc = web.Load("http://www.manhuajia.cn/author/");
        HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@id='update_list']/ol/li/a");
        foreach (HtmlNode link in nodes)
        {
            string[] zzmcArr = link.InnerText.Split(new string[] { "(日)", "(内)", "(韩国)", "(港台)", "(欧美)", "(观看人数" }, StringSplitOptions.None);
            string zzmc = zzmcArr[zzmcArr.Length - 2].Trim();
            string zzlink = link.Attributes["href"].Value;
            if (DbHelperSQL.Exists("select top(1) id from zz where zzmc='" + zzmc + "' or zzbm='" + zzmc + "'"))
	        {
                doc = web.Load(zzlink);
                HtmlNode img = doc.DocumentNode.SelectSingleNode("//div[@class='mh-pic mt-10']/center/img");
                if (img != null && !img.Attributes["src"].Value.Contains("no_avatar.jpg"))
                {
                    #region 保存封面
                    string filename = System.IO.Path.GetFileName(img.Attributes["src"].Value);
                    if (!System.IO.File.Exists(Server.MapPath("~/cpic/" + filename)))
                    {
                        WebClient wc = new WebClient();
                        byte[] b = wc.DownloadData(img.Attributes["src"].Value);
                        System.IO.MemoryStream ms = new System.IO.MemoryStream(YuuzleComic.Common.Input.ResizeImageFile(b, 120, 160));
                        System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
                        try
                        {
                            image.Save(Server.MapPath("~/zpic/" + filename), System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                        catch (Exception)
                        {
                            #region 解决gdi+一般性错误
                            Bitmap newBmp = new Bitmap(image);
                            newBmp.Save(Server.MapPath("~/zpic/" + filename), System.Drawing.Imaging.ImageFormat.Jpeg);
                            newBmp.Dispose();
                            #endregion
                        }
                        DbHelperSQL.ExecuteSql("update zz set zztp='" + filename + "' where id in (select id from zz where zzmc='" + zzmc + "' or zzbm='" + zzmc + "')");
                    }
                    
                    #endregion
                }
	        }
            
        }
        Response.Write("<script>alert('采集完成！')</script>");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string authorList = "http://www.xindm.cn/zuozhe/";
        DataTable diqudt = diqubll.GetAllList().Tables[0];

        WebClient wc = new WebClient();
        wc.Encoding = Encoding.GetEncoding("gb2312");
        string s = wc.DownloadString(authorList);
        Parser parser = Parser.CreateParser(s, "utf-8");
        AndFilter andFilter = new AndFilter(new TagNameFilter("div"), new HasAttributeFilter("class", "authorcontents"));
        NodeList divList = parser.ExtractAllNodesThatMatch(andFilter);
        if (divList.Count > 0)
        {
            for (int i = 0; i < divList.Count; i++)
            {
                andFilter = new AndFilter(new TagNameFilter("div"), new HasAttributeFilter("class", "letternoa red"));
                NodeList areaList = divList[i].Children.ExtractAllNodesThatMatch(andFilter);
                DataRow[] drArr = diqudt.Select("diqu='" + areaList[0].ToPlainTextString().Replace("漫画家", "") + "'");

                Parser parserA = Parser.CreateParser(divList[i].ToHtml(), "utf-8");
                andFilter = new AndFilter(new TagNameFilter("a"), new HasAttributeFilter("href"));
                NodeList aTagList = parserA.ExtractAllNodesThatMatch(andFilter);
                for (int f = 0; f < aTagList.Count; f++)
                {
                    ATag atag = (ATag)aTagList[f];
                    try
                    {
                        YuuzleComic.Model.zz model = new YuuzleComic.Model.zz();
                        if (drArr.Length > 0)
                        {
                            model.dqid = Convert.ToInt32(drArr[0]["id"]);
                        }
                        else
                        {
                            model.dqid = 3;
                        }
                        model.zzmc = atag.ToPlainTextString().Trim();
                        model.zztp = "none.jpg";
                        //model.zzjs = authorcontent.Children.ExtractAllNodesThatMatch(new HasAttributeFilter("class", "authorabout allfloatright gray"))[0].Children.ToHtml();
                        model.zm = YuuzleComic.Common.Chs2Spell.GetHeadOfChs(model.zzmc.Substring(0, 1));
                        if (zzbll.Exists(model.zzmc))
                        {
                            model.id = zzbll.GetIdByName(model.zzmc);
                            DbHelperSQL.ExecuteSql("update zz set dqid=" + drArr[0]["id"] + " where id=" + model.id + " and dqid is null");
                        }
                        else
                        {
                            zzbll.Add(model);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message == "远程服务器返回错误: (404) 未找到。")
                        {
                            continue;
                        }
                        else
                        {
                            Response.Write(ex.Message);
                        }
                    }
                }

            }
        }
        Response.Write("<script>alert('采集完成！')</script>");
    }
}